import 'package:flutter/material.dart';

class TabDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var choices = const <Choice>[
      Choice('自驾', Icons.directions_car),
      Choice('自行车', Icons.directions_bike),
      Choice('乘船', Icons.directions_boat),
      Choice('公交车', Icons.directions_bus),
      Choice('火车', Icons.directions_railway),
      Choice('步行', Icons.directions_walk)
    ];

    return DefaultTabController(
      length: choices.length,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Tab控件'),
          bottom: TabBar(
              tabs: choices
                  .map((Choice choice) => Tab(
                        text: choice.title,
                        icon: Icon(choice.icon),
                      ))
                  .toList()),
        ),
        body: TabBarView(
            children: choices
                .map((Choice choice) => Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Center(
                        child: Column(
                          mainAxisSize: MainAxisSize.min, //主轴占用的空间
                          crossAxisAlignment: CrossAxisAlignment.center, //水平居中
                          children: <Widget>[
                            Icon(
                              choice.icon,
                              size: 128.0,
                              color:
                                  Theme.of(context).textTheme.headline4.color,
                            ),
                            Text(
                              choice.title,
                              style: Theme.of(context).textTheme.headline4,
                            )
                          ],
                        ),
                      ),
                    ))
                .toList()),
      ),
    );
  }
}

class Choice {
  const Choice(this.title, this.icon);

  final String title;
  final IconData icon;
}
